"""
两个界面跳转
"""
from socket import socket

HOST = "127.0.0.1"
PORT = 8000
ADDR = (HOST, PORT)


class DictHandle:
    def __init__(self):
        self.sock = self._create_socket()

    def _create_socket(self):
        sock = socket()
        sock.connect(ADDR)
        return sock

    def register(self, name, password):
        # 验证用户名和密码没有空格
        if " " in name or " " in password:
            print("用户名或者密码不能有空格")
            return
        mes = "R " + name + " " + password
        self.sock.send(mes.encode())
        result = self.sock.recv(1024)
        if result == b'ok':
            print('注册成功')
        else:
            print("注册失败")

    def exit(self):
        self.sock.send(b'E')

    def login(self, name, password):
        mes = "L " + name + " " + password
        self.sock.send(mes.encode())
        result = self.sock.recv(1024)
        if result == b'ok':
            print('登录成功')
            # self.name = name
            return True
        else:
            print("登录失败")
            return False

    def query(self):
        while True:
            words = input("请输入你要查询的单词：")
            if words=="##":
                break
            mes = "Q " + words
            self.sock.send(mes.encode())
            result = self.sock.recv(1024)
            # 无论接受到什么都打印
            print("%s:%s" % (words, result.decode()))

    def history(self):
        mes = "H "
        self.sock.send(mes.encode())
        result = self.sock.recv(1024)
        print(result.decode())


class DictVIew:
    def __init__(self):
        self.handle = DictHandle()

    def __input(self):
        name = input("请输入姓名：")
        password = input("请输入与密码:")
        return name, password

    def __menu_1(self):
        while True:
            print("""
                =========== Welcome =============
                  1. 注册    2. 登录     3. 退出
                =================================
                """)
            cmd = input("请输入选项:")
            if cmd == '1':
                name, password = self.__input()
                self.handle.register(name, password)

            elif cmd == '2':
                name, password = self.__input()
                reuslt = self.handle.login(name, password)
                if reuslt:
                    self.__menu_2()  # 二级界面
            elif cmd == '3':
                self.handle.exit()
                print("退出")
                break
            else:
                print("请输入正确选项!")

    def __menu_2(self):
        while True:
            print("""
                =============== Query ===============
                  1. 查单词    2. 历史记录    3. 注销
                =====================================
                    """)
            cmd = input("请输入选项:")
            if cmd == '1':
                self.handle.query()
            elif cmd == '2':
                self.handle.history()
            elif cmd == '3':
                break  # 结束二级界面
            else:
                print("请输入正确选项!")

    def main(self):
        self.__menu_1()


if __name__ == '__main__':
    dict = DictVIew()
    dict.main()
